const mongoose = require('mongoose');
var express = require('express')
var xueke = express()

const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const {xuekeService } = require('../../service/impl/xuekeService/index');

/* 
  let fills ={
    "key1":{
      "path":"kemuid",
    },
    "key2":{
      "path":"xuekeid",
   }
  }

*/


xueke.post('/post/xueke/add', async (req,res) => {
  let result
  try {
    let news  = req.body.news
    result =  await xuekeService.getIns().create(news)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  xueke.post('/post/xueke/delete', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      result =  await xuekeService.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  xueke.post('/post/xueke/update', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      let updates = req.body.updates
      result =  await xuekeService.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  xueke.post('/post/xueke/query', async (req,res) => {
    let result
    try {
      result =  await xuekeService.getIns().find()
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  module.exports= xueke